package com.base.engine.graphics;

import static org.lwjgl.opengl.GL11.* ; 
import static org.lwjgl.util.glu.GLU.* ; 

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;

import com.base.engine.system.MADSystem;


public final class MADBasicGL {

	public static final float NEAR_CLIPPING = 0.001f; 
	public static final float FAR_CLIPPING = 5000.0f; 
	public static final Color4f CLEAR_COLOR = new Color4f(0,0,0,1); 
	
	public static final void initGL(){
		
		glClearColor(CLEAR_COLOR.getRed(), CLEAR_COLOR.getGreen(), CLEAR_COLOR.getBlue(), CLEAR_COLOR.getAlpha()); 
		glClearDepth(1.0f);
		
		switch3D(); 
		glEnable(GL_TEXTURE_2D);
		
		glShadeModel(GL_SMOOTH);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
		glEnable(GL_LINE_SMOOTH);
		glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); 
		glEnable(GL_POLYGON_SMOOTH);

		glEnable(GL_CULL_FACE); 
		glCullFace(GL_BACK) ; 
		
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		
		glEnable(GL_ALPHA_TEST);
		glAlphaFunc(GL_GREATER, 0.001f) ;
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_BLEND);

		glEnable(GL_LIGHT0);
		glEnable(GL_LIGHTING);
		
//		glLightModel(GL_LIGHT_MODEL_AMBIENT, MADSystem.asFloatBuffer(new float[]{0.05f, 0.05f, 0.05f, 1.0f})); 
//		glLight(GL_LIGHT0, GL_DIFFUSE, MADSystem.asFloatBuffer(new float[]{0.5f, 0.5f, 0.5f, 0.5f})); 
//		
		glEnable(GL_COLOR_MATERIAL); 
		glColorMaterial(GL_FRONT, GL_DIFFUSE); 
		
	}
	
	public static final void exitGL(){
		glDisable(GL_DEPTH_TEST); 
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_LIGHT1);
	}
	
	public static void switch2D(float left, float top, float width, float height){
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity(); 
		gluOrtho2D(left, width, height, top); 
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity(); 
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_CULL_FACE);
	}
	public static void switch3D(){
		glViewport(0,0,Display.getWidth(), Display.getHeight());
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0f, (float)Display.getWidth()/(float)Display.getHeight(), NEAR_CLIPPING, FAR_CLIPPING);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity(); 
		glDepthFunc(GL_LEQUAL);
		glEnable(GL_DEPTH_TEST);
		glCullFace(GL_FRONT);
		glEnable(GL_CULL_FACE);
		
	}
	
}
